package flc.ast.fragment;

import android.app.Dialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.NoteActivity;
import flc.ast.adapter.NoteAdapter;
import flc.ast.bean.NoteBean;
import flc.ast.databinding.FragmentNotepadBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link NotepadFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NotepadFragment extends BaseNoModelFragment<FragmentNotepadBinding> {

    private List<NoteBean> listShow = new ArrayList<>();
    private NoteAdapter noteAdapter;
    private boolean isEdit = false;
    private Dialog myDeleteDialog;
    private int selPosition;


    @Override
    protected int onCreate() {
        return R.layout.fragment_notepad;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivNotepadAdd.setOnClickListener(this);
        mDataBinding.ivAddNote.setOnClickListener(this);
        mDataBinding.tvNotepadEdit.setOnClickListener(this);

        mDataBinding.rvNoteList.setLayoutManager(new LinearLayoutManager(mContext));
        noteAdapter = new NoteAdapter();
        mDataBinding.rvNoteList.setAdapter(noteAdapter);
        noteAdapter.addChildClickViewIds(R.id.ivNoteItemDelete);
        noteAdapter.setOnItemChildClickListener(this);

        //删除弹框
        deleteDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivNotepadAdd://添加记事本
            case R.id.ivAddNote:
                startActivity(NoteActivity.class);
                break;
            case R.id.tvNotepadEdit://编辑
                if (isEdit) {
                    isEdit = false;
                    mDataBinding.tvNotepadEdit.setText(getString(R.string.edit));
                } else {
                    isEdit = true;
                    mDataBinding.tvNotepadEdit.setText(getString(R.string.cancel));
                }
                noteAdapter.setEdit(isEdit);
                noteAdapter.notifyDataSetChanged();
                break;
            //删除弹框
            case R.id.ivDialogDeleteCancel://取消
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteRight://确认
                List<NoteBean> listSp = AppSpUtils.getNoteList();
                listSp.remove(selPosition);
                AppSpUtils.saveNoteList(listSp);
                myDeleteDialog.dismiss();
                getData();
                ToastUtils.showShort(getString(R.string.delete_suc));
                break;

        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivNoteItemDelete://删除
                selPosition = position;
                myDeleteDialog.show();
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<NoteBean> listSp = AppSpUtils.getNoteList();
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            noteAdapter.setList(listShow);
            mDataBinding.rvNoteList.setVisibility(View.VISIBLE);
            mDataBinding.llNoteNoData.setVisibility(View.GONE);
            mDataBinding.tvNotepadEdit.setVisibility(View.VISIBLE);
            mDataBinding.ivNotepadAdd.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvNoteList.setVisibility(View.GONE);
            mDataBinding.llNoteNoData.setVisibility(View.VISIBLE);
            mDataBinding.tvNotepadEdit.setVisibility(View.GONE);
            mDataBinding.ivNotepadAdd.setVisibility(View.GONE);
            isEdit = false;
            mDataBinding.tvNotepadEdit.setText(getString(R.string.edit));
        }
    }

    //删除弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.CENTER);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        lp.width = (int) (dm.widthPixels * 1);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogCancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView dialogRight = inflate.findViewById(R.id.ivDialogDeleteRight);
        TextView tip = inflate.findViewById(R.id.tvDialogDeleteTip);
        tip.setText(getString(R.string.is_delete_note));
        dialogCancel.setOnClickListener(this);
        dialogRight.setOnClickListener(this);
    }

}